package com.evan.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import org.apache.http.util.ByteArrayBuffer;

import android.graphics.Bitmap;
import android.os.Environment;

public class FilesUtil {

	public static String sd_card = Environment.getExternalStorageDirectory()
			.getAbsolutePath();

	private static String IMAGE_FROM_NET_DIR_PATH, TEMP_IMAGE_DIR_PATH,
			TEMP_VOICE_DIR_PATH;

	public static void setNetImageDir(String path) {
		IMAGE_FROM_NET_DIR_PATH = path;
	}

	public static void setTempImageDir(String path) {
		TEMP_IMAGE_DIR_PATH = path;
	}

	public static void setVoiceDIr(String path) {
		TEMP_VOICE_DIR_PATH = path;
	}

	/**
	 * 创建文件夹
	 * 
	 * @param context
	 */
	public static void mkdir() {
		createDir(IMAGE_FROM_NET_DIR_PATH);
		createDir(TEMP_IMAGE_DIR_PATH);
		createDir(TEMP_VOICE_DIR_PATH);
	}

	public static void createDir(String path) {
		if (!CommonUtility.isNull(path)) {
			File file = new File(sd_card + path);
			if (!file.exists()) {
				file.mkdirs();
			}
		}
	}

	public static String getTempVoicePath() {
		return getUUIDVoicePath();
	}

	public static String getUUIDImgPath() {
		return sd_card + TEMP_IMAGE_DIR_PATH + UUID.randomUUID().toString()
				+ ".png.cache";
	}

	private static String getUUIDVoicePath() {
		return sd_card + TEMP_VOICE_DIR_PATH + UUID.randomUUID().toString()
				+ ".amr";
	}

	public static String getRootPath() {
		return sd_card + IMAGE_FROM_NET_DIR_PATH;
	}

	/**
	 * 保存图片到SD卡
	 * 
	 * @param URL
	 * @param data
	 * @throws IOException
	 */
	public static void saveImage(String fileName, byte[] data)
			throws IOException {
		saveData(getRootPath(), fileName, data);
	}

	/**
	 * 读取图片
	 * 
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static byte[] readImage(String filename) throws IOException {
		byte[] tmp = readData(getRootPath(), filename);
		return tmp;
	}

	/**
	 * 读取图片工具
	 * 
	 * @param path
	 * @param name
	 * @return
	 * @throws IOException
	 */
	private static byte[] readData(String path, String name) throws IOException {
		// String name = MyHash.mixHashStr(url);
		String paths = path + name;
		File file = new File(paths);
		if (!file.exists()) {
			return null;
		}
		InputStream inputstream = new FileInputStream(file);
		ByteArrayBuffer buffer = new ByteArrayBuffer(1024);
		byte[] tmp = new byte[1024];
		int len;
		while (((len = inputstream.read(tmp)) != -1)) {
			buffer.append(tmp, 0, len);
		}
		inputstream.close();
		return buffer.toByteArray();
	}

	/**
	 * 图片保存工具类
	 * 
	 * @param path
	 * @param fileName
	 * @param data
	 * @throws IOException
	 */
	private static void saveData(String path, String fileName, byte[] data)
			throws IOException {
		// String name = MyHash.mixHashStr(AdName);
		File file = new File(path + fileName);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outStream = new FileOutputStream(file);
		outStream.write(data);
		outStream.close();
		file = null;
		data = null;
	}

	/**
	 * method desc：
	 * 
	 * @param path
	 *            absolute file path
	 * @param bitmap
	 */
	public static boolean saveData(String path, Bitmap bitmap) {
		// String name = MyHash.mixHashStr(AdName);
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				fileOut.close();
				fileOut = null;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!CommonUtility.isNull(bitmap) && !bitmap.isRecycled()) {
				bitmap.recycle();
				bitmap = null;
			}
		}
		return true;
	}

	/**
	 * method desc：
	 * 
	 * @param path
	 *            absolute file path
	 * @param bitmap
	 */
	public static boolean saveData(String path, Bitmap bitmap, boolean isRecyle) {
		// String name = MyHash.mixHashStr(AdName);
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				fileOut.close();
				fileOut = null;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!CommonUtility.isNull(bitmap) && !bitmap.isRecycled()
					&& isRecyle) {
				bitmap.recycle();
				bitmap = null;
			}
		}
		return true;
	}

	/**
	 * 判断文件是否存在 true存在 false不存在
	 * 
	 * @param url
	 * @return
	 */
	public static boolean compare(String fileName) {
		String paths = getRootPath() + fileName;
		File file = new File(paths);
		if (!file.exists()) {
			return false;
		}
		return true;
	}

	public static boolean exist(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return false;
		}
		return true;
	}

	public static void deleteFile(String dir, String path) {
		dir = dir == null ? "" : dir;
		if (!CommonUtility.isNull(path)) {
			File file = new File(path);
			if (!file.exists() && file.isFile()) {
				file.delete();
			}
		}
	}

	public static void deleteFile(String dir) {
		if (!CommonUtility.isNull(dir)) {
			File file = new File(dir);
			if (file.isDirectory()) {
				File[] childFile = file.listFiles();
				for (File f : childFile) {
					if (f.exists() && f.isFile()) {
						f.delete();
					}
				}
			}
		}
	}

}
